package com.zombie.logic.core.box2d;

import com.badlogic.gdx.physics.box2d.Fixture;
import com.zombie.logic.enums.ObjectType;
import com.zombie.logic.object.Bullet;
import com.zombie.logic.object.GameObject;
import com.zombie.logic.object.LiveObject;

public class ContactFilter implements com.badlogic.gdx.physics.box2d.ContactFilter {

	public boolean shouldCollide(Fixture a, Fixture b){
		GameObject o1,o2;
		o1 = (GameObject) a.getBody().getUserData();
		o2 = (GameObject) b.getBody().getUserData();
		if (o1 != null && o2 != null){
			if (o1.owner == o2 || o2.owner == o1)
				return false;
			else if (o1.type == ObjectType.BULLET && o2.type == ObjectType.BULLET)
				return false;
			else if (o1.type == ObjectType.EXPLOSIVE && o2.type == ObjectType.EXPLOSIVE)
				return false;
		}

		if (o1 != null && o1.type == ObjectType.BULLET)
			return bulletCheck(o1,o2);
		else if (o2 != null && o2.type == ObjectType.BULLET)
			return bulletCheck(o2,o1);
		return true;
	}

	private boolean bulletCheck(GameObject bullet, GameObject object) {
		if (bullet.type == ObjectType.BULLET)
			if (((Bullet) bullet).strongBullet && ((Bullet) bullet).lastIntersected != null &&
					((Bullet) bullet).lastIntersected == object)
				return false;
			else if (object != null && object.type == ObjectType.LIVE && ((LiveObject) object).isDead())
				return false;
		return true;
	}
}
